package com.jsb.laundry.common.utils;

import com.jsb.laundry.model.vo.ConfirmOrderPageVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderCommonUtil {

    private static final long serialVersionUID = -1185015143654744142L;

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderCommonUtil.class);

    private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM-dd");
    private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");

    /**
     * 生成预约时间
     * @param currentDateTime
     * @return
     */
    public static List<ConfirmOrderPageVo.CurrentTimeVo> getTime(Date currentDateTime) {
        LOGGER.info("获取当前时间后7天" + currentDateTime);

        // 将Date转换为Instant，并使用默认时区创建LocalDateTime
        LocalDateTime now = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());

        List<ConfirmOrderPageVo.CurrentTimeVo> returnList = new ArrayList<>();

        for (int i = 0; i < 7; i++) {
            ConfirmOrderPageVo.CurrentTimeVo currentTimeResult = new ConfirmOrderPageVo.CurrentTimeVo();
            LocalDateTime currentDay = now.plusDays(i);
            currentTimeResult.setText(dateFormatter.format(currentDay));
            currentTimeResult.setValue(currentDay.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() - 1);

            int k = currentDay.getHour();
            for (int j = 0; j < 24 - currentDay.getHour(); ) {

                currentDay = currentDay.withMonth(currentDay.getMonthValue()).withDayOfMonth(currentDay.getDayOfMonth()).withHour(8).withMinute(0);

                ConfirmOrderPageVo.CurrentTimeVo var1 = new ConfirmOrderPageVo.CurrentTimeVo();

                LocalDateTime currentHours = currentDay.plusHours(j);
                j += 2;

                var1.setText(timeFormatter.format(currentHours) + " - " + timeFormatter.format(currentDay.plusHours(j)));
                var1.setValue(currentHours.plusHours(1).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());

                //配送时间(8:00~22:00)
                if (currentHours.getHour() >= 22 || currentHours.getHour() < 8) {
                    break;
                } else {
                    currentTimeResult.getChildren().add(var1);
                }

            }
            returnList.add(currentTimeResult);
        }
        long time = currentDateTime.getTime();
        ConfirmOrderPageVo.CurrentTimeVo currentTimeVo = returnList.get(0);
        Long currentTimeStamp = currentTimeVo.getValue();
        List<ConfirmOrderPageVo.CurrentTimeVo> children = currentTimeVo.getChildren();
        int index = -1;
        for (int i = 0; i < children.size(); i++) {
            if (children.get(i).getValue() >= currentTimeStamp) { //找到当前时间所处的时间段
                index = i - 1;
                break;
            }
        }
        if (index == -1) {
            returnList.remove(0);
        } else {
            List<ConfirmOrderPageVo.CurrentTimeVo> subChildren = children.subList(index, children.size());
            currentTimeVo.setValue(time - 1);
            subChildren.get(0).setValue(time);
            currentTimeVo.setChildren(subChildren);
            returnList.set(0, currentTimeVo);
        }
        return returnList;
    }

}
